Match = {}

State = {}
State.FirstHalf = 1
State.SecondHalf = 2
State.OverTime = 3

local CurrentState = State.FirstHalf

function Match.Start()
	PrintMessage(HUD_PRINTTALK, "Start 1 Half")
	CurrentState = State.FirstHalf
	Half.Start()
end

function Match.End(winnerTeam)
	gamemode.Call("MatchEnd", winnerTeam)
end

function Match.CheckForWin()
	if team.GetScore(DsTeam.Blue) > team.GetScore(DsTeam.Red) then
		Match.End(DsTeam.Blue)
		return true
	elseif team.GetScore(DsTeam.Red) > team.GetScore(DsTeam.Blue) then
		Match.End(DsTeam.Red)
		return true
	end
	return false
end

hook.Add("TimeUp", "Match.OnTimeUp", function()
	if CurrentState == State.FirstHalf then
		PrintMessage(HUD_PRINTTALK, "Start 2 Half")
		CurrentState = State.SecondHalf
		timer.Simple(5, Half.Start)
	elseif CurrentState == State.SecondHalf then
		if Match.CheckForWin() then
			PrintMessage(HUD_PRINTTALK, "Match End.")
		else
			PrintMessage(HUD_PRINTTALK, "Overtime!")
			CurrentState = State.OverTime
			timer.Simple(5, Round.Start)			
		end
	end
end)

hook.Add("Goal", "Match.OnGoal", function()
	PrintMessage(HUD_PRINTTALK, "Goal?")
	if CurrentState == State.OverTime then
		Match.CheckForWin()
	end
end)